<!doctype html>
<html>
<head>
<meta charset='UTF-8'><meta name='viewport' content='width=device-width initial-scale=1'>
<title>解析比特币数据</title><link href='https://fonts.loli.net/css?family=Open+Sans:400italic,700italic,700,400&subset=latin,latin-ext' rel='stylesheet' type='text/css' /><style type='text/css'>html {overflow-x: initial !important;}:root { --bg-color: #ffffff; --text-color: #333333; --select-text-bg-color: #B5D6FC; --select-text-font-color: auto; --monospace: "Lucida Console",Consolas,"Courier",monospace; }
html { font-size: 14px; background-color: var(--bg-color); color: var(--text-color); font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; }
body { margin: 0px; padding: 0px; height: auto; bottom: 0px; top: 0px; left: 0px; right: 0px; font-size: 1rem; line-height: 1.42857143; overflow-x: hidden; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: inherit; tab-size: 4; background-position: inherit inherit; background-repeat: inherit inherit; }
iframe { margin: auto; }
a.url { word-break: break-all; }
a:active, a:hover { outline: 0px; }
.in-text-selection, ::selection { text-shadow: none; background: var(--select-text-bg-color); color: var(--select-text-font-color); }
#write { margin: 0px auto; height: auto; width: inherit; word-break: normal; word-wrap: break-word; position: relative; white-space: normal; overflow-x: visible; padding-top: 40px; }
#write.first-line-indent p { text-indent: 2em; }
#write.first-line-indent li p, #write.first-line-indent p * { text-indent: 0px; }
#write.first-line-indent li { margin-left: 2em; }
.for-image #write { padding-left: 8px; padding-right: 8px; }
body.typora-export { padding-left: 30px; padding-right: 30px; }
.typora-export .footnote-line, .typora-export li, .typora-export p { white-space: pre-wrap; }
@media screen and (max-width: 500px) { 
  body.typora-export { padding-left: 0px; padding-right: 0px; }
  #write { padding-left: 20px; padding-right: 20px; }
  .CodeMirror-sizer { margin-left: 0px !important; }
  .CodeMirror-gutters { display: none !important; }
}
#write li > figure:last-child { margin-bottom: 0.5rem; }
#write ol, #write ul { position: relative; }
img { max-width: 100%; vertical-align: middle; }
button, input, select, textarea { color: inherit; font-family: inherit; font-size: inherit; font-style: inherit; font-variant-caps: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; }
input[type="checkbox"], input[type="radio"] { line-height: normal; padding: 0px; }
*, ::after, ::before { box-sizing: border-box; }
#write h1, #write h2, #write h3, #write h4, #write h5, #write h6, #write p, #write pre { width: inherit; }
#write h1, #write h2, #write h3, #write h4, #write h5, #write h6, #write p { position: relative; }
p { line-height: inherit; }
h1, h2, h3, h4, h5, h6 { break-after: avoid-page; break-inside: avoid; orphans: 2; }
p { orphans: 4; }
h1 { font-size: 2rem; }
h2 { font-size: 1.8rem; }
h3 { font-size: 1.6rem; }
h4 { font-size: 1.4rem; }
h5 { font-size: 1.2rem; }
h6 { font-size: 1rem; }
.md-math-block, .md-rawblock, h1, h2, h3, h4, h5, h6, p { margin-top: 1rem; margin-bottom: 1rem; }
.hidden { display: none; }
.md-blockmeta { color: rgb(204, 204, 204); font-weight: 700; font-style: italic; }
a { cursor: pointer; }
sup.md-footnote { padding: 2px 4px; background-color: rgba(238, 238, 238, 0.701961); color: rgb(85, 85, 85); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; cursor: pointer; }
sup.md-footnote a, sup.md-footnote a:hover { color: inherit; text-transform: inherit; text-decoration: inherit; }
#write input[type="checkbox"] { cursor: pointer; width: inherit; height: inherit; }
figure { overflow-x: auto; margin: 1.2em 0px; max-width: calc(100% + 16px); padding: 0px; }
figure > table { margin: 0px !important; }
tr { break-inside: avoid; break-after: auto; }
thead { display: table-header-group; }
table { border-collapse: collapse; border-spacing: 0px; width: 100%; overflow: auto; break-inside: auto; text-align: left; }
table.md-table td { min-width: 32px; }
.CodeMirror-gutters { border-right-width: 0px; background-color: inherit; }
.CodeMirror-linenumber { }
.CodeMirror { text-align: left; }
.CodeMirror-placeholder { opacity: 0.3; }
.CodeMirror pre { padding: 0px 4px; }
.CodeMirror-lines { padding: 0px; }
div.hr:focus { cursor: none; }
#write pre { white-space: pre-wrap; }
#write.fences-no-line-wrapping pre { white-space: pre; }
#write pre.ty-contain-cm { white-space: normal; }
.CodeMirror-gutters { margin-right: 4px; }
.md-fences { font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; overflow: visible; white-space: pre; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: inherit; position: relative !important; background-position: inherit inherit; background-repeat: inherit inherit; }
.md-diagram-panel { width: 100%; margin-top: 10px; text-align: center; padding-top: 0px; padding-bottom: 8px; overflow-x: auto; }
#write .md-fences.mock-cm { white-space: pre-wrap; }
.md-fences.md-fences-with-lineno { padding-left: 0px; }
#write.fences-no-line-wrapping .md-fences.mock-cm { white-space: pre; overflow-x: auto; }
.md-fences.mock-cm.md-fences-with-lineno { padding-left: 8px; }
.CodeMirror-line, twitterwidget { break-inside: avoid; }
.footnotes { opacity: 0.8; font-size: 0.9rem; margin-top: 1em; margin-bottom: 1em; }
.footnotes + .footnotes { margin-top: 0px; }
.md-reset { margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: top; text-decoration: none; text-shadow: none; float: none; position: static; width: auto; height: auto; white-space: nowrap; cursor: inherit; line-height: normal; font-weight: 400; text-align: left; box-sizing: content-box; direction: ltr; background-position: 0px 0px; background-repeat: initial initial; }
li div { padding-top: 0px; }
blockquote { margin: 1rem 0px; }
li .mathjax-block, li p { margin: 0.5rem 0px; }
li { margin: 0px; position: relative; }
blockquote > :last-child { margin-bottom: 0px; }
blockquote > :first-child, li > :first-child { margin-top: 0px; }
.footnotes-area { color: rgb(136, 136, 136); margin-top: 0.714rem; padding-bottom: 0.143rem; white-space: normal; }
#write .footnote-line { white-space: pre-wrap; }
@media print { 
  body, html { border: 1px solid transparent; height: 99%; break-after: avoid-page; break-before: avoid-page; }
  #write { margin-top: 0px; padding-top: 0px; border-color: transparent !important; }
  .typora-export * { -webkit-print-color-adjust: exact; }
  html.blink-to-pdf { font-size: 13px; }
  .typora-export #write { padding-left: 32px; padding-right: 32px; padding-bottom: 0px; break-after: avoid-page; }
  .typora-export #write::after { height: 0px; }
}
.footnote-line { margin-top: 0.714em; font-size: 0.7em; }
a img, img a { cursor: pointer; }
pre.md-meta-block { font-size: 0.8rem; min-height: 0.8rem; white-space: pre-wrap; background-color: rgb(204, 204, 204); display: block; overflow-x: hidden; background-position: initial initial; background-repeat: initial initial; }
p > .md-image:only-child:not(.md-img-error) img, p > img:only-child { display: block; margin: auto; }
p > .md-image:only-child { display: inline-block; width: 100%; }
#write .MathJax_Display { margin: 0.8em 0px 0px; }
.md-math-block { width: 100%; }
.md-math-block:not(:empty)::after { display: none; }
[contenteditable="true"]:active, [contenteditable="true"]:focus { outline: 0px; box-shadow: none; }
.md-task-list-item { position: relative; list-style-type: none; }
.task-list-item.md-task-list-item { padding-left: 0px; }
.md-task-list-item > input { position: absolute; top: 0px; left: 0px; margin-left: -1.2em; margin-top: calc(1em - 10px); border: none; }
.math { font-size: 1rem; }
.md-toc { min-height: 3.58rem; position: relative; font-size: 0.9rem; border-top-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; border-bottom-left-radius: 10px; }
.md-toc-content { position: relative; margin-left: 0px; }
.md-toc-content::after, .md-toc::after { display: none; }
.md-toc-item { display: block; color: rgb(65, 131, 196); }
.md-toc-item a { text-decoration: none; }
.md-toc-inner:hover { text-decoration: underline; }
.md-toc-inner { display: inline-block; cursor: pointer; }
.md-toc-h1 .md-toc-inner { margin-left: 0px; font-weight: 700; }
.md-toc-h2 .md-toc-inner { margin-left: 2em; }
.md-toc-h3 .md-toc-inner { margin-left: 4em; }
.md-toc-h4 .md-toc-inner { margin-left: 6em; }
.md-toc-h5 .md-toc-inner { margin-left: 8em; }
.md-toc-h6 .md-toc-inner { margin-left: 10em; }
@media screen and (max-width: 48em) { 
  .md-toc-h3 .md-toc-inner { margin-left: 3.5em; }
  .md-toc-h4 .md-toc-inner { margin-left: 5em; }
  .md-toc-h5 .md-toc-inner { margin-left: 6.5em; }
  .md-toc-h6 .md-toc-inner { margin-left: 8em; }
}
a.md-toc-inner { font-size: inherit; font-style: inherit; font-weight: inherit; line-height: inherit; }
.footnote-line a:not(.reversefootnote) { color: inherit; }
.md-attr { display: none; }
.md-fn-count::after { content: "."; }
code, pre, samp, tt { font-family: var(--monospace); }
kbd { margin: 0px 0.1em; padding: 0.1em 0.6em; font-size: 0.8em; color: rgb(36, 39, 41); background-color: rgb(255, 255, 255); border: 1px solid rgb(173, 179, 185); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; box-shadow: rgba(12, 13, 14, 0.2) 0px 1px 0px, rgb(255, 255, 255) 0px 0px 0px 2px inset; white-space: nowrap; vertical-align: middle; background-position: initial initial; background-repeat: initial initial; }
.md-comment { color: rgb(162, 127, 3); opacity: 0.8; font-family: var(--monospace); }
code { text-align: left; }
a.md-print-anchor { white-space: pre !important; border: none !important; display: inline-block !important; position: absolute !important; width: 1px !important; right: 0px !important; outline: 0px !important; text-shadow: initial !important; background-position: 0px 0px !important; background-repeat: initial initial !important; }
.md-inline-math .MathJax_SVG .noError { display: none !important; }
.html-for-mac .inline-math-svg .MathJax_SVG { vertical-align: 0.2px; }
.md-math-block .MathJax_SVG_Display { text-align: center; margin: 0px; position: relative; text-indent: 0px; max-width: none; max-height: none; min-height: 0px; min-width: 100%; width: auto; overflow-y: hidden; display: block !important; }
.MathJax_SVG_Display, .md-inline-math .MathJax_SVG_Display { width: auto; margin: inherit; display: inline-block !important; }
.MathJax_SVG .MJX-monospace { font-family: var(--monospace); }
.MathJax_SVG .MJX-sans-serif { font-family: sans-serif; }
.MathJax_SVG { display: inline; font-style: normal; font-weight: 400; line-height: normal; zoom: 90%; text-indent: 0px; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin: 0px; }
.MathJax_SVG * { transition: none; }
.MathJax_SVG_Display svg { vertical-align: middle !important; margin-bottom: 0px !important; margin-top: 0px !important; }
.os-windows.monocolor-emoji .md-emoji { font-family: "Segoe UI Symbol", sans-serif; }
.md-diagram-panel > svg { max-width: 100%; }
[lang="mermaid"] svg, [lang="flow"] svg { max-width: 100%; height: auto; }
[lang="mermaid"] .node text { font-size: 1rem; }
table tr th { border-bottom-width: 0px; }
video { max-width: 100%; display: block; margin: 0px auto; }
iframe { max-width: 100%; width: 100%; border: none; }
.highlight td, .highlight tr { border: 0px; }


.CodeMirror { height: auto; }
.CodeMirror.cm-s-inner { background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: inherit; background-position: inherit inherit; background-repeat: inherit inherit; }
.CodeMirror-scroll { overflow-y: hidden; overflow-x: auto; z-index: 3; }
.CodeMirror-gutter-filler, .CodeMirror-scrollbar-filler { background-color: rgb(255, 255, 255); }
.CodeMirror-gutters { border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: inherit; white-space: nowrap; background-position: inherit inherit; background-repeat: inherit inherit; }
.CodeMirror-linenumber { padding: 0px 3px 0px 5px; text-align: right; color: rgb(153, 153, 153); }
.cm-s-inner .cm-keyword { color: rgb(119, 0, 136); }
.cm-s-inner .cm-atom, .cm-s-inner.cm-atom { color: rgb(34, 17, 153); }
.cm-s-inner .cm-number { color: rgb(17, 102, 68); }
.cm-s-inner .cm-def { color: rgb(0, 0, 255); }
.cm-s-inner .cm-variable { color: rgb(0, 0, 0); }
.cm-s-inner .cm-variable-2 { color: rgb(0, 85, 170); }
.cm-s-inner .cm-variable-3 { color: rgb(0, 136, 85); }
.cm-s-inner .cm-string { color: rgb(170, 17, 17); }
.cm-s-inner .cm-property { color: rgb(0, 0, 0); }
.cm-s-inner .cm-operator { color: rgb(152, 26, 26); }
.cm-s-inner .cm-comment, .cm-s-inner.cm-comment { color: rgb(170, 85, 0); }
.cm-s-inner .cm-string-2 { color: rgb(255, 85, 0); }
.cm-s-inner .cm-meta { color: rgb(85, 85, 85); }
.cm-s-inner .cm-qualifier { color: rgb(85, 85, 85); }
.cm-s-inner .cm-builtin { color: rgb(51, 0, 170); }
.cm-s-inner .cm-bracket { color: rgb(153, 153, 119); }
.cm-s-inner .cm-tag { color: rgb(17, 119, 0); }
.cm-s-inner .cm-attribute { color: rgb(0, 0, 204); }
.cm-s-inner .cm-header, .cm-s-inner.cm-header { color: rgb(0, 0, 255); }
.cm-s-inner .cm-quote, .cm-s-inner.cm-quote { color: rgb(0, 153, 0); }
.cm-s-inner .cm-hr, .cm-s-inner.cm-hr { color: rgb(153, 153, 153); }
.cm-s-inner .cm-link, .cm-s-inner.cm-link { color: rgb(0, 0, 204); }
.cm-negative { color: rgb(221, 68, 68); }
.cm-positive { color: rgb(34, 153, 34); }
.cm-header, .cm-strong { font-weight: 700; }
.cm-del { text-decoration: line-through; }
.cm-em { font-style: italic; }
.cm-link { text-decoration: underline; }
.cm-error { color: red; }
.cm-invalidchar { color: red; }
.cm-constant { color: rgb(38, 139, 210); }
.cm-defined { color: rgb(181, 137, 0); }
div.CodeMirror span.CodeMirror-matchingbracket { color: rgb(0, 255, 0); }
div.CodeMirror span.CodeMirror-nonmatchingbracket { color: rgb(255, 34, 34); }
.cm-s-inner .CodeMirror-activeline-background { background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: inherit; background-position: inherit inherit; background-repeat: inherit inherit; }
.CodeMirror { position: relative; overflow: hidden; }
.CodeMirror-scroll { height: 100%; outline: 0px; position: relative; box-sizing: content-box; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: inherit; background-position: inherit inherit; background-repeat: inherit inherit; }
.CodeMirror-sizer { position: relative; }
.CodeMirror-gutter-filler, .CodeMirror-hscrollbar, .CodeMirror-scrollbar-filler, .CodeMirror-vscrollbar { position: absolute; z-index: 6; display: none; }
.CodeMirror-vscrollbar { right: 0px; top: 0px; overflow: hidden; }
.CodeMirror-hscrollbar { bottom: 0px; left: 0px; overflow: hidden; }
.CodeMirror-scrollbar-filler { right: 0px; bottom: 0px; }
.CodeMirror-gutter-filler { left: 0px; bottom: 0px; }
.CodeMirror-gutters { position: absolute; left: 0px; top: 0px; padding-bottom: 30px; z-index: 3; }
.CodeMirror-gutter { white-space: normal; height: 100%; box-sizing: content-box; padding-bottom: 30px; margin-bottom: -32px; display: inline-block; }
.CodeMirror-gutter-wrapper { position: absolute; z-index: 4; border: none !important; background-position: 0px 0px !important; background-repeat: initial initial !important; }
.CodeMirror-gutter-background { position: absolute; top: 0px; bottom: 0px; z-index: 4; }
.CodeMirror-gutter-elt { position: absolute; cursor: default; z-index: 4; }
.CodeMirror-lines { cursor: text; }
.CodeMirror pre { border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; border-width: 0px; font-family: inherit; font-size: inherit; margin: 0px; white-space: pre; word-wrap: normal; color: inherit; z-index: 2; position: relative; overflow: visible; background-position: 0px 0px; background-repeat: initial initial; }
.CodeMirror-wrap pre { word-wrap: break-word; white-space: pre-wrap; word-break: normal; }
.CodeMirror-code pre { border-right-width: 30px; border-right-style: solid; border-right-color: transparent; width: fit-content; }
.CodeMirror-wrap .CodeMirror-code pre { border-right-style: none; width: auto; }
.CodeMirror-linebackground { position: absolute; left: 0px; right: 0px; top: 0px; bottom: 0px; z-index: 0; }
.CodeMirror-linewidget { position: relative; z-index: 2; overflow: auto; }
.CodeMirror-wrap .CodeMirror-scroll { overflow-x: hidden; }
.CodeMirror-measure { position: absolute; width: 100%; height: 0px; overflow: hidden; visibility: hidden; }
.CodeMirror-measure pre { position: static; }
.CodeMirror div.CodeMirror-cursor { position: absolute; visibility: hidden; border-right-style: none; width: 0px; }
.CodeMirror div.CodeMirror-cursor { visibility: hidden; }
.CodeMirror-focused div.CodeMirror-cursor { visibility: inherit; }
.cm-searching { background-color: rgba(255, 255, 0, 0.4); background-position: initial initial; background-repeat: initial initial; }
@media print { 
  .CodeMirror div.CodeMirror-cursor { visibility: hidden; }
}


:root {
    --side-bar-bg-color: #fafafa;
    --control-text-color: #777;
}

@include-when-export url(https://fonts.loli.net/css?family=Open+Sans:400italic,700italic,700,400&subset=latin,latin-ext);

html {
    font-size: 16px;
}

body {
    font-family: "Open Sans","Clear Sans","Helvetica Neue",Helvetica,Arial,sans-serif;
    color: rgb(51, 51, 51);
    line-height: 1.6;
}

#write {
    max-width: 860px;
  	margin: 0 auto;
  	padding: 30px;
    padding-bottom: 100px;
}
#write > ul:first-child,
#write > ol:first-child{
    margin-top: 30px;
}

a {
    color: #4183C4;
}
h1,
h2,
h3,
h4,
h5,
h6 {
    position: relative;
    margin-top: 1rem;
    margin-bottom: 1rem;
    font-weight: bold;
    line-height: 1.4;
    cursor: text;
}
h1:hover a.anchor,
h2:hover a.anchor,
h3:hover a.anchor,
h4:hover a.anchor,
h5:hover a.anchor,
h6:hover a.anchor {
    text-decoration: none;
}
h1 tt,
h1 code {
    font-size: inherit;
}
h2 tt,
h2 code {
    font-size: inherit;
}
h3 tt,
h3 code {
    font-size: inherit;
}
h4 tt,
h4 code {
    font-size: inherit;
}
h5 tt,
h5 code {
    font-size: inherit;
}
h6 tt,
h6 code {
    font-size: inherit;
}
h1 {
    padding-bottom: .3em;
    font-size: 2.25em;
    line-height: 1.2;
    border-bottom: 1px solid #eee;
}
h2 {
   padding-bottom: .3em;
    font-size: 1.75em;
    line-height: 1.225;
    border-bottom: 1px solid #eee;
}
h3 {
    font-size: 1.5em;
    line-height: 1.43;
}
h4 {
    font-size: 1.25em;
}
h5 {
    font-size: 1em;
}
h6 {
   font-size: 1em;
    color: #777;
}
p,
blockquote,
ul,
ol,
dl,
table{
    margin: 0.8em 0;
}
li>ol,
li>ul {
    margin: 0 0;
}
hr {
    height: 2px;
    padding: 0;
    margin: 16px 0;
    background-color: #e7e7e7;
    border: 0 none;
    overflow: hidden;
    box-sizing: content-box;
}

li p.first {
    display: inline-block;
}
ul,
ol {
    padding-left: 30px;
}
ul:first-child,
ol:first-child {
    margin-top: 0;
}
ul:last-child,
ol:last-child {
    margin-bottom: 0;
}
blockquote {
    border-left: 4px solid #dfe2e5;
    padding: 0 15px;
    color: #777777;
}
blockquote blockquote {
    padding-right: 0;
}
table {
    padding: 0;
    word-break: initial;
}
table tr {
    border-top: 1px solid #dfe2e5;
    margin: 0;
    padding: 0;
}
table tr:nth-child(2n),
thead {
    background-color: #f8f8f8;
}
table tr th {
    font-weight: bold;
    border: 1px solid #dfe2e5;
    border-bottom: 0;
    text-align: left;
    margin: 0;
    padding: 6px 13px;
}
table tr td {
    border: 1px solid #dfe2e5;
    text-align: left;
    margin: 0;
    padding: 6px 13px;
}
table tr th:first-child,
table tr td:first-child {
    margin-top: 0;
}
table tr th:last-child,
table tr td:last-child {
    margin-bottom: 0;
}

.CodeMirror-lines {
    padding-left: 4px;
}

.code-tooltip {
    box-shadow: 0 1px 1px 0 rgba(0,28,36,.3);
    border-top: 1px solid #eef2f2;
}

.md-fences,
code,
tt {
    border: 1px solid #e7eaed;
    background-color: #f8f8f8;
    border-radius: 3px;
    padding: 0;
    padding: 2px 4px 0px 4px;
    font-size: 0.9em;
}

code {
    background-color: #f3f4f4;
    padding: 0 2px 0 2px;
}

.md-fences {
    margin-bottom: 15px;
    margin-top: 15px;
    padding-top: 8px;
    padding-bottom: 6px;
}


.md-task-list-item > input {
  margin-left: -1.3em;
}

@media print {
    html {
        font-size: 13px;
    }
    table,
    pre {
        page-break-inside: avoid;
    }
    pre {
        word-wrap: break-word;
    }
}

.md-fences {
	background-color: #f8f8f8;
}
#write pre.md-meta-block {
	padding: 1rem;
    font-size: 85%;
    line-height: 1.45;
    background-color: #f7f7f7;
    border: 0;
    border-radius: 3px;
    color: #777777;
    margin-top: 0 !important;
}

.mathjax-block>.code-tooltip {
	bottom: .375rem;
}

.md-mathjax-midline {
    background: #fafafa;
}

#write>h3.md-focus:before{
	left: -1.5625rem;
	top: .375rem;
}
#write>h4.md-focus:before{
	left: -1.5625rem;
	top: .285714286rem;
}
#write>h5.md-focus:before{
	left: -1.5625rem;
	top: .285714286rem;
}
#write>h6.md-focus:before{
	left: -1.5625rem;
	top: .285714286rem;
}
.md-image>.md-meta {
    /*border: 1px solid #ddd;*/
    border-radius: 3px;
    padding: 2px 0px 0px 4px;
    font-size: 0.9em;
    color: inherit;
}

.md-tag {
    color: #a7a7a7;
    opacity: 1;
}

.md-toc { 
    margin-top:20px;
    padding-bottom:20px;
}

.sidebar-tabs {
    border-bottom: none;
}

#typora-quick-open {
    border: 1px solid #ddd;
    background-color: #f8f8f8;
}

#typora-quick-open-item {
    background-color: #FAFAFA;
    border-color: #FEFEFE #e5e5e5 #e5e5e5 #eee;
    border-style: solid;
    border-width: 1px;
}

/** focus mode */
.on-focus-mode blockquote {
    border-left-color: rgba(85, 85, 85, 0.12);
}

header, .context-menu, .megamenu-content, footer{
    font-family: "Segoe UI", "Arial", sans-serif;
}

.file-node-content:hover .file-node-icon,
.file-node-content:hover .file-node-open-state{
    visibility: visible;
}

.mac-seamless-mode #typora-sidebar {
    background-color: #fafafa;
    background-color: var(--side-bar-bg-color);
}

.md-lang {
    color: #b4654d;
}

.html-for-mac .context-menu {
    --item-hover-bg-color: #E6F0FE;
}

#md-notification .btn {
    border: 0;
}

.dropdown-menu .divider {
    border-color: #e5e5e5;
}

.ty-preferences .window-content {
    background-color: #fafafa;
}

.ty-preferences .nav-group-item.active {
    color: white;
    background: #999;
}

 .typora-export li, .typora-export p, .typora-export,  .footnote-line {white-space: normal;} 
</style>
</head>
<body class='typora-export' >
<div  id='write'  class = 'is-mac'><h2><a name="深入理解-ocap-实现-(1):-如何解析-bitcoin-的数据" class="md-header-anchor"></a><span> 深入理解 OCAP 实现 (1): 如何解析 Bitcoin 的数据</span></h2><p><span>2018-08-16</span></p><p><strong><span>原文地址：</span></strong><span>   </span></p><p><a href='https://www.arcblock.io/zh/post/2018/08/16/index-bitcoin' target='_blank' class='url'>https://www.arcblock.io/zh/post/2018/08/16/index-bitcoin</a></p><p><strong><span>作者：</span></strong><span> 丁沛灵 （ArcBlock 团队软件工程师）</span></p><p><span>为了帮助大家更深入地理解 ArcBlock 的开放链访问协议的实现和技术细节，我们的工程团队将定期写技术文章来“解密”我们在设计和开发 OCAP 过程中的一些技术细节。 希望读者能借此了解 OCAP 背后的设计思路和实现细节，也许你可以参与来实现更多的 OCAP 链适配器，支持更多的区块链协议。 也欢迎大家来指出我们设计中的不足，在讨论中完善我们的设计。</span></p><h2><a name="[![img](data:image/svg+xml,%3csvg-xmlns='http://www.w3.org/2000/svg'-aria-hidden='true'-focusable='false'-height='16'-version='1.1'-viewbox='0-0-16-16'-width='16'-data-evernote-id='54'-class='js-evernote-checked'%3e%3cpath-fill-rule='evenodd'-d='m4-9h1v1h4c-1.5-0-3-1.69-3-3.5s2.55-3-4-3h4c1.45-0-3-1.69-3-3.5-0-1.41-.91-2.72-2-3.25v8.59c.58-.45-1-1.27-1-2.09c10-5.22-8.98-4-8-4h4c-.98-0-2-1.22-2-2.5s3-9-4-9zm9-3h-1v1h1c1-0-2-1.22-2-2.5s13.98-12-13-12h9c-.98-0-2-1.22-2-2.5-0-.83.42-1.64-1-2.09v6.25c-1.09.53-2-1.84-2-3.25c6-11.31-7.55-13-9-13h4c1.45-0-3-1.69-3-3.5s14.5-6-13-6z'-data-evernote-id='55'-class='js-evernote-checked'%3e%3c/path%3e%3c/svg%3e)](https://app.yinxiang.com/outboundredirect.action?dest=https%3a%2f%2fwww.arcblock.io%2fzh%2fpost%2f2018%2f08%2f16%2findex-bitcoin%23%e4%b8%ba%e4%bb%80%e4%b9%88%e8%a6%81%e8%a7%a3%e6%9e%90-bitcoin-%e6%95%b0%e6%8d%ae%ef%bc%9f)为什么要解析-bitcoin-数据？" class="md-header-anchor"></a><a href='https://app.yinxiang.com/OutboundRedirect.action?dest=https%3A%2F%2Fwww.arcblock.io%2Fzh%2Fpost%2F2018%2F08%2F16%2Findex-bitcoin%23%E4%B8%BA%E4%BB%80%E4%B9%88%E8%A6%81%E8%A7%A3%E6%9E%90-bitcoin-%E6%95%B0%E6%8D%AE%EF%BC%9F'><img src='data:image/svg+xml,%3csvg xmlns=&#39;http://www.w3.org/2000/svg&#39; aria-hidden=&#39;true&#39; focusable=&#39;false&#39; height=&#39;16&#39; version=&#39;1.1&#39; viewBox=&#39;0 0 16 16&#39; width=&#39;16&#39; data-evernote-id=&#39;54&#39; class=&#39;js-evernote-checked&#39;%3e%3cpath fill-rule=&#39;evenodd&#39; d=&#39;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&#39; data-evernote-id=&#39;55&#39; class=&#39;js-evernote-checked&#39;%3e%3c/path%3e%3c/svg%3e' alt='img' referrerPolicy='no-referrer' /></a><span>为什么要解析 Bitcoin 数据？</span></h2><p><span>我们在七月底如期发布了 OCAP 的第一版本。在这一版本中，OCAP 提供了针对 Bitcoin 数据的查询服务。用户不仅可以快速的通过 Hash 拿到某一个 Block 或者 Transaction 的信息，并且还可以做复杂的级联查询，比如查询特定的两个地址之间的交易。下面的这个 query 就可以返回那个著名的 Pizza 交易</span></p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang=""><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang=""><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">{</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">  transactionsByAddress(sender: "17SkEw2md5avVNyYgj6RiXuQKNwkXaxFyQ", receiver: "13TETb2WMr58mexBaNq1jmXV1J7Abk2tE2") {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  data {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;  blockHeight</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;  hash</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;  total</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;  numberInputs</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;  numberOutputs</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 264px;"></div><div class="CodeMirror-gutters" style="display: none; height: 264px;"></div></div></div></pre><p><span>原生的 Bitcoin API 并不支持这样的数据查询，所以想要让 OCAP 具备这样的查询功能，我们就必须预处理 Bitcoin 上的数据，让它以我们想要的方式存储。这也就引出了我们今天的主题 -- 如何解析 Bitcoin 数据？</span></p><h2><a name="[![img](data:image/svg+xml,%3csvg-xmlns='http://www.w3.org/2000/svg'-aria-hidden='true'-focusable='false'-height='16'-version='1.1'-viewbox='0-0-16-16'-width='16'-data-evernote-id='59'-class='js-evernote-checked'%3e%3cpath-fill-rule='evenodd'-d='m4-9h1v1h4c-1.5-0-3-1.69-3-3.5s2.55-3-4-3h4c1.45-0-3-1.69-3-3.5-0-1.41-.91-2.72-2-3.25v8.59c.58-.45-1-1.27-1-2.09c10-5.22-8.98-4-8-4h4c-.98-0-2-1.22-2-2.5s3-9-4-9zm9-3h-1v1h1c1-0-2-1.22-2-2.5s13.98-12-13-12h9c-.98-0-2-1.22-2-2.5-0-.83.42-1.64-1-2.09v6.25c-1.09.53-2-1.84-2-3.25c6-11.31-7.55-13-9-13h4c1.45-0-3-1.69-3-3.5s14.5-6-13-6z'-data-evernote-id='60'-class='js-evernote-checked'%3e%3c/path%3e%3c/svg%3e)](https://app.yinxiang.com/outboundredirect.action?dest=https%3a%2f%2fwww.arcblock.io%2fzh%2fpost%2f2018%2f08%2f16%2findex-bitcoin%23%e6%a6%82%e8%bf%b0)概述" class="md-header-anchor"></a><a href='https://app.yinxiang.com/OutboundRedirect.action?dest=https%3A%2F%2Fwww.arcblock.io%2Fzh%2Fpost%2F2018%2F08%2F16%2Findex-bitcoin%23%E6%A6%82%E8%BF%B0'><img src='data:image/svg+xml,%3csvg xmlns=&#39;http://www.w3.org/2000/svg&#39; aria-hidden=&#39;true&#39; focusable=&#39;false&#39; height=&#39;16&#39; version=&#39;1.1&#39; viewBox=&#39;0 0 16 16&#39; width=&#39;16&#39; data-evernote-id=&#39;59&#39; class=&#39;js-evernote-checked&#39;%3e%3cpath fill-rule=&#39;evenodd&#39; d=&#39;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&#39; data-evernote-id=&#39;60&#39; class=&#39;js-evernote-checked&#39;%3e%3c/path%3e%3c/svg%3e' alt='img' referrerPolicy='no-referrer' /></a><span>概述</span></h2><p><span>在讲技术细节之前我们先从整体上理解一下我们要干什么，为了说清楚一点，我们可以类比搜索引擎。</span></p><p><span>众所周知，互联网上的数据纷繁复杂，而搜索引擎却能在海量的数据里面迅速的查找出用户想要的结果。其之所以能够做到这一点是因为搜索引擎的后面还有两个默默付出的组件 -- 爬虫和倒排索引。 爬虫负责不停的从互联网上搜集数据，倒排索引负责将数据以特定的形式存储，以便搜索引擎能够快速的查询。</span></p><p><span>同样的，ArcBlock OCAP 所支持的区块链查询就相当于提供针对区块链的“搜索引擎”服务，也少不了类似的预处理过程。不同于搜索引擎，我们不需要一个爬虫，因为 Bitcoin 的数据都是以二进制的形式存在节点的磁盘上的。但是数据是以一种对人不太友好的方式组织在一起的，所以我们首先需要一个解析器，将这些二进制数据读出来，还原成它们本来的面目。之后我们再对这些数据进行一些加工，就得到了 OCAP 所需要的数据。</span></p><h2><a name="[![img](data:image/svg+xml,%3csvg-xmlns='http://www.w3.org/2000/svg'-aria-hidden='true'-focusable='false'-height='16'-version='1.1'-viewbox='0-0-16-16'-width='16'-data-evernote-id='65'-class='js-evernote-checked'%3e%3cpath-fill-rule='evenodd'-d='m4-9h1v1h4c-1.5-0-3-1.69-3-3.5s2.55-3-4-3h4c1.45-0-3-1.69-3-3.5-0-1.41-.91-2.72-2-3.25v8.59c.58-.45-1-1.27-1-2.09c10-5.22-8.98-4-8-4h4c-.98-0-2-1.22-2-2.5s3-9-4-9zm9-3h-1v1h1c1-0-2-1.22-2-2.5s13.98-12-13-12h9c-.98-0-2-1.22-2-2.5-0-.83.42-1.64-1-2.09v6.25c-1.09.53-2-1.84-2-3.25c6-11.31-7.55-13-9-13h4c1.45-0-3-1.69-3-3.5s14.5-6-13-6z'-data-evernote-id='66'-class='js-evernote-checked'%3e%3c/path%3e%3c/svg%3e)](https://app.yinxiang.com/outboundredirect.action?dest=https%3a%2f%2fwww.arcblock.io%2fzh%2fpost%2f2018%2f08%2f16%2findex-bitcoin%23%e6%8a%80%e6%9c%af%e7%bb%86%e8%8a%82)技术细节" class="md-header-anchor"></a><a href='https://app.yinxiang.com/OutboundRedirect.action?dest=https%3A%2F%2Fwww.arcblock.io%2Fzh%2Fpost%2F2018%2F08%2F16%2Findex-bitcoin%23%E6%8A%80%E6%9C%AF%E7%BB%86%E8%8A%82'><img src='data:image/svg+xml,%3csvg xmlns=&#39;http://www.w3.org/2000/svg&#39; aria-hidden=&#39;true&#39; focusable=&#39;false&#39; height=&#39;16&#39; version=&#39;1.1&#39; viewBox=&#39;0 0 16 16&#39; width=&#39;16&#39; data-evernote-id=&#39;65&#39; class=&#39;js-evernote-checked&#39;%3e%3cpath fill-rule=&#39;evenodd&#39; d=&#39;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&#39; data-evernote-id=&#39;66&#39; class=&#39;js-evernote-checked&#39;%3e%3c/path%3e%3c/svg%3e' alt='img' referrerPolicy='no-referrer' /></a><span>技术细节</span></h2><p><span>在这里我会主要谈谈 Bitcoin 的数据具体是怎么存储的，并且在得到这些数据之后还需要做哪些额外的计算。</span></p><h3><a name="[![img](data:image/svg+xml,%3csvg-xmlns='http://www.w3.org/2000/svg'-aria-hidden='true'-focusable='false'-height='16'-version='1.1'-viewbox='0-0-16-16'-width='16'-data-evernote-id='69'-class='js-evernote-checked'%3e%3cpath-fill-rule='evenodd'-d='m4-9h1v1h4c-1.5-0-3-1.69-3-3.5s2.55-3-4-3h4c1.45-0-3-1.69-3-3.5-0-1.41-.91-2.72-2-3.25v8.59c.58-.45-1-1.27-1-2.09c10-5.22-8.98-4-8-4h4c-.98-0-2-1.22-2-2.5s3-9-4-9zm9-3h-1v1h1c1-0-2-1.22-2-2.5s13.98-12-13-12h9c-.98-0-2-1.22-2-2.5-0-.83.42-1.64-1-2.09v6.25c-1.09.53-2-1.84-2-3.25c6-11.31-7.55-13-9-13h4c1.45-0-3-1.69-3-3.5s14.5-6-13-6z'-data-evernote-id='70'-class='js-evernote-checked'%3e%3c/path%3e%3c/svg%3e)](https://app.yinxiang.com/outboundredirect.action?dest=https%3a%2f%2fwww.arcblock.io%2fzh%2fpost%2f2018%2f08%2f16%2findex-bitcoin%23bitcoin-%e6%95%b0%e6%8d%ae%e6%98%af%e5%a6%82%e4%bd%95%e5%ad%98%e5%82%a8%e7%9a%84)bitcoin-数据是如何存储的" class="md-header-anchor"></a><a href='https://app.yinxiang.com/OutboundRedirect.action?dest=https%3A%2F%2Fwww.arcblock.io%2Fzh%2Fpost%2F2018%2F08%2F16%2Findex-bitcoin%23bitcoin-%E6%95%B0%E6%8D%AE%E6%98%AF%E5%A6%82%E4%BD%95%E5%AD%98%E5%82%A8%E7%9A%84'><img src='data:image/svg+xml,%3csvg xmlns=&#39;http://www.w3.org/2000/svg&#39; aria-hidden=&#39;true&#39; focusable=&#39;false&#39; height=&#39;16&#39; version=&#39;1.1&#39; viewBox=&#39;0 0 16 16&#39; width=&#39;16&#39; data-evernote-id=&#39;69&#39; class=&#39;js-evernote-checked&#39;%3e%3cpath fill-rule=&#39;evenodd&#39; d=&#39;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&#39; data-evernote-id=&#39;70&#39; class=&#39;js-evernote-checked&#39;%3e%3c/path%3e%3c/svg%3e' alt='img' referrerPolicy='no-referrer' /></a><span>Bitcoin 数据是如何存储的</span></h3><h4><a name="[![img](data:image/svg+xml,%3csvg-xmlns='http://www.w3.org/2000/svg'-aria-hidden='true'-focusable='false'-height='16'-version='1.1'-viewbox='0-0-16-16'-width='16'-data-evernote-id='72'-class='js-evernote-checked'%3e%3cpath-fill-rule='evenodd'-d='m4-9h1v1h4c-1.5-0-3-1.69-3-3.5s2.55-3-4-3h4c1.45-0-3-1.69-3-3.5-0-1.41-.91-2.72-2-3.25v8.59c.58-.45-1-1.27-1-2.09c10-5.22-8.98-4-8-4h4c-.98-0-2-1.22-2-2.5s3-9-4-9zm9-3h-1v1h1c1-0-2-1.22-2-2.5s13.98-12-13-12h9c-.98-0-2-1.22-2-2.5-0-.83.42-1.64-1-2.09v6.25c-1.09.53-2-1.84-2-3.25c6-11.31-7.55-13-9-13h4c1.45-0-3-1.69-3-3.5s14.5-6-13-6z'-data-evernote-id='73'-class='js-evernote-checked'%3e%3c/path%3e%3c/svg%3e)](https://app.yinxiang.com/outboundredirect.action?dest=https%3a%2f%2fwww.arcblock.io%2fzh%2fpost%2f2018%2f08%2f16%2findex-bitcoin%23%e5%ad%98%e5%82%a8%e6%95%b0%e6%8d%ae%e7%9a%84%e6%96%87%e4%bb%b6)存储数据的文件" class="md-header-anchor"></a><a href='https://app.yinxiang.com/OutboundRedirect.action?dest=https%3A%2F%2Fwww.arcblock.io%2Fzh%2Fpost%2F2018%2F08%2F16%2Findex-bitcoin%23%E5%AD%98%E5%82%A8%E6%95%B0%E6%8D%AE%E7%9A%84%E6%96%87%E4%BB%B6'><img src='data:image/svg+xml,%3csvg xmlns=&#39;http://www.w3.org/2000/svg&#39; aria-hidden=&#39;true&#39; focusable=&#39;false&#39; height=&#39;16&#39; version=&#39;1.1&#39; viewBox=&#39;0 0 16 16&#39; width=&#39;16&#39; data-evernote-id=&#39;72&#39; class=&#39;js-evernote-checked&#39;%3e%3cpath fill-rule=&#39;evenodd&#39; d=&#39;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&#39; data-evernote-id=&#39;73&#39; class=&#39;js-evernote-checked&#39;%3e%3c/path%3e%3c/svg%3e' alt='img' referrerPolicy='no-referrer' /></a><span>存储数据的文件</span></h4><p><span>Bitcoin 的原始数据可以在$HOME/.bitcoin/blocks 下面找到。这个目录下面主要有两类文件，一种是类似于这样的文件：blk00952.dat， 而另一种是类似于这样的文件：rev000952.dat。 前一种 blk 开头的文件就是存储 Bitcoin 源数据的文件，而后一种是用来做 rewind 的。关于 rewind 我们下回再表，我们这次的重点在源数据文件。每一个 blk 数据文件大小的上限都是 128MB。当一个文件写满后，Bitcoin 程序会新建一个类似的文件来存储新收到的区块信息。</span></p><p><span>让我们先通过如下命令，来看看这些文件里面都存了什么。</span></p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" style="page-break-inside: unset;"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang=""><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">od -x --endian=big -N 297 -An blk00000.dat</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> f9be b4d9 1d01 0000 0100 0000 0000 0000</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> 0000 0000 0000 0000 0000 0000 0000 0000</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> 0000 0000 0000 0000 0000 0000 3ba3 edfd</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> 7a7b 12b2 7ac7 2c3e 6776 8f61 7fc8 1bc3</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> 888a 5132 3a9f b8aa 4b1e 5e4a 29ab 5f49</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> ffff 001d 1dac 2b7c 0101 0000 0001 0000</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> 0000 0000 0000 0000 0000 0000 0000 0000</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> 0000 0000 0000 0000 0000 0000 0000 ffff</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> ffff 4d04 ffff 001d 0104 4554 6865 2054</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> 696d 6573 2030 332f 4a61 6e2f 3230 3039</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> 2043 6861 6e63 656c 6c6f 7220 6f6e 2062</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> 7269 6e6b 206f 6620 7365 636f 6e64 2062</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> 6169 6c6f 7574 2066 6f72 2062 616e 6b73</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> ffff ffff 0100 f205 2a01 0000 0043 4104</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> 678a fdb0 fe55 4827 1967 f1a6 7130 b710</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> 5cd6 a828 e039 09a6 7962 e0ea 1f61 deb6</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> 49f6 bc3f 4cef 38c4 f355 04e5 1ec1 12de</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> 5c38 4df7 ba0b 8d57 8a4c 702b 6bf1 1d5f</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> ac00 0000 00f9 beb4 d900</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 440px;"></div><div class="CodeMirror-gutters" style="display: none; height: 440px;"></div></div></div></pre><p><span>上面的文件就是 Bitcoin 的 Genesis BLock。它是二进制数据（以 16 进制表示），每两个字符表示一个字节。乍一看感觉乱七八糟看不懂，但是当你了解他们的数据格式之后，一切都不是那么难。</span></p><h4><a name="[![img](data:image/svg+xml,%3csvg-xmlns='http://www.w3.org/2000/svg'-aria-hidden='true'-focusable='false'-height='16'-version='1.1'-viewbox='0-0-16-16'-width='16'-data-evernote-id='78'-class='js-evernote-checked'%3e%3cpath-fill-rule='evenodd'-d='m4-9h1v1h4c-1.5-0-3-1.69-3-3.5s2.55-3-4-3h4c1.45-0-3-1.69-3-3.5-0-1.41-.91-2.72-2-3.25v8.59c.58-.45-1-1.27-1-2.09c10-5.22-8.98-4-8-4h4c-.98-0-2-1.22-2-2.5s3-9-4-9zm9-3h-1v1h1c1-0-2-1.22-2-2.5s13.98-12-13-12h9c-.98-0-2-1.22-2-2.5-0-.83.42-1.64-1-2.09v6.25c-1.09.53-2-1.84-2-3.25c6-11.31-7.55-13-9-13h4c1.45-0-3-1.69-3-3.5s14.5-6-13-6z'-data-evernote-id='79'-class='js-evernote-checked'%3e%3c/path%3e%3c/svg%3e)](https://app.yinxiang.com/outboundredirect.action?dest=https%3a%2f%2fwww.arcblock.io%2fzh%2fpost%2f2018%2f08%2f16%2findex-bitcoin%23%e6%95%b0%e6%8d%ae%e7%9a%84%e5%ad%98%e5%82%a8%e7%bb%93%e6%9e%84)数据的存储结构" class="md-header-anchor"></a><a href='https://app.yinxiang.com/OutboundRedirect.action?dest=https%3A%2F%2Fwww.arcblock.io%2Fzh%2Fpost%2F2018%2F08%2F16%2Findex-bitcoin%23%E6%95%B0%E6%8D%AE%E7%9A%84%E5%AD%98%E5%82%A8%E7%BB%93%E6%9E%84'><img src='data:image/svg+xml,%3csvg xmlns=&#39;http://www.w3.org/2000/svg&#39; aria-hidden=&#39;true&#39; focusable=&#39;false&#39; height=&#39;16&#39; version=&#39;1.1&#39; viewBox=&#39;0 0 16 16&#39; width=&#39;16&#39; data-evernote-id=&#39;78&#39; class=&#39;js-evernote-checked&#39;%3e%3cpath fill-rule=&#39;evenodd&#39; d=&#39;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&#39; data-evernote-id=&#39;79&#39; class=&#39;js-evernote-checked&#39;%3e%3c/path%3e%3c/svg%3e' alt='img' referrerPolicy='no-referrer' /></a><span>数据的存储结构</span></h4><p><span>Bitcoin 官网上有关于数据格式的具体讲解，但是他们的文档的组织结构并不容易阅读理解，所以我们自己总结了一个更直观的图表。</span></p><p><a href='https://app.yinxiang.com/OutboundRedirect.action?dest=https%3A%2F%2Fwww.arcblock.io%2Fstatic%2F623285bed64c01d3961b9d9cd794337a%2F9d9b4%2Fblock_structure.png'><img src='https://app.yinxiang.com/shard/s6/nl/1035675/66aa3a8e-7c17-4f35-bd2c-b586a64ce40b/res/2853a2fc-d971-47ca-baa5-a902dbba2ec3/block_structure.png?resizeSmall&amp;width=832' alt='区块数据的结构' referrerPolicy='no-referrer' /></a></p><p><span>对图的说明：</span></p><ol><li><p><span>这个图代表一个区块文件，而一个 blkxxxx.data 文件正是由一个个这样的区块组成的。</span></p></li><li><p><span>在上图中，蓝色代表复合数据类型，橙色代表简单数据类型，绿色是注解。比如一个区块文件由 Preamble 和 Block 组成。而一个 Block 由一个 Block Header,Number of Transactions 和若干个 Transaction 组成。每个 Transaction 又由几个简单数据类型和若干个 Transaction Input/Ouput 组成。以此类推。</span></p></li><li><p><span>几乎所有数据都是以小端类型（注：Little-Endian，下同）存储的，但是 Transaction Input/Output 里的</span><code>script</code><span>除外。它们是以大端方式（注：Big-Endian，下同）存储的。</span></p></li><li><p><code>magic number</code><span>是一个固定的数，其值为 0xD9B4BEF9</span></p></li><li><p><code>variable integer</code><span>本身的长度不定，在 1 到 9 字节之间。具体的解析规则如下：</span></p><ul><li><span>如果第一个字节小于 253，那么直接返回该字节所代表的数值</span></li><li><span>如果第一个字节等于 253，那么往后再读一个字节作，将其值作为返回值</span></li><li><span>如果第一个字节等于 254，那么往后再读四个字节作，将其值作为返回值</span></li><li><span>如果第一个字节等于 255，那么往后再读八个字节作，将其值作为返回值</span></li></ul></li></ol><p><span>如果我们把之前的数据按照上述规则带入的话可以得到如下图表。</span></p><p><a href='https://app.yinxiang.com/OutboundRedirect.action?dest=https%3A%2F%2Fwww.arcblock.io%2Fstatic%2F6d9b5192129724680b547963e6423ed9%2F55006%2Ffilled_block_structure.png'><img src='https://app.yinxiang.com/shard/s6/nl/1035675/66aa3a8e-7c17-4f35-bd2c-b586a64ce40b/res/5e597215-2a5a-44fb-a984-fe1a4a8d4812/filled_block_structure.png?resizeSmall&amp;width=832' alt='filled block structure' referrerPolicy='no-referrer' /></a></p><p><span>怎么样，现在是不是清楚多了？注意，我并有没把数据换成大端格式，请自行转换。</span></p><h3><a name="[![img](data:image/svg+xml,%3csvg-xmlns='http://www.w3.org/2000/svg'-aria-hidden='true'-focusable='false'-height='16'-version='1.1'-viewbox='0-0-16-16'-width='16'-data-evernote-id='103'-class='js-evernote-checked'%3e%3cpath-fill-rule='evenodd'-d='m4-9h1v1h4c-1.5-0-3-1.69-3-3.5s2.55-3-4-3h4c1.45-0-3-1.69-3-3.5-0-1.41-.91-2.72-2-3.25v8.59c.58-.45-1-1.27-1-2.09c10-5.22-8.98-4-8-4h4c-.98-0-2-1.22-2-2.5s3-9-4-9zm9-3h-1v1h1c1-0-2-1.22-2-2.5s13.98-12-13-12h9c-.98-0-2-1.22-2-2.5-0-.83.42-1.64-1-2.09v6.25c-1.09.53-2-1.84-2-3.25c6-11.31-7.55-13-9-13h4c1.45-0-3-1.69-3-3.5s14.5-6-13-6z'-data-evernote-id='104'-class='js-evernote-checked'%3e%3c/path%3e%3c/svg%3e)](https://app.yinxiang.com/outboundredirect.action?dest=https%3a%2f%2fwww.arcblock.io%2fzh%2fpost%2f2018%2f08%2f16%2findex-bitcoin%23%e8%ae%a1%e7%ae%97%e9%a2%9d%e5%a4%96%e5%ad%97%e6%ae%b5)计算额外字段" class="md-header-anchor"></a><a href='https://app.yinxiang.com/OutboundRedirect.action?dest=https%3A%2F%2Fwww.arcblock.io%2Fzh%2Fpost%2F2018%2F08%2F16%2Findex-bitcoin%23%E8%AE%A1%E7%AE%97%E9%A2%9D%E5%A4%96%E5%AD%97%E6%AE%B5'><img src='data:image/svg+xml,%3csvg xmlns=&#39;http://www.w3.org/2000/svg&#39; aria-hidden=&#39;true&#39; focusable=&#39;false&#39; height=&#39;16&#39; version=&#39;1.1&#39; viewBox=&#39;0 0 16 16&#39; width=&#39;16&#39; data-evernote-id=&#39;103&#39; class=&#39;js-evernote-checked&#39;%3e%3cpath fill-rule=&#39;evenodd&#39; d=&#39;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&#39; data-evernote-id=&#39;104&#39; class=&#39;js-evernote-checked&#39;%3e%3c/path%3e%3c/svg%3e' alt='img' referrerPolicy='no-referrer' /></a><span>计算额外字段</span></h3><p><span>细心的同学可能已经注意到了，这些数据中不包括几个非常重要的字段，Block Hash，Block Height，Transaction Hash 以及 Addresses。不错，中本聪在设计 Bitcoin 的时候可谓是极度的“抠门”，能通过计算得出的数据绝对不存在链上。从链的设计角度来看，这样做是绝对正确的，它能节省许多磁盘空间，但是这却给我们带来了额外的工作。下面我们分别谈谈各个字段的计算。</span></p><h4><a name="[![img](data:image/svg+xml,%3csvg-xmlns='http://www.w3.org/2000/svg'-aria-hidden='true'-focusable='false'-height='16'-version='1.1'-viewbox='0-0-16-16'-width='16'-data-evernote-id='107'-class='js-evernote-checked'%3e%3cpath-fill-rule='evenodd'-d='m4-9h1v1h4c-1.5-0-3-1.69-3-3.5s2.55-3-4-3h4c1.45-0-3-1.69-3-3.5-0-1.41-.91-2.72-2-3.25v8.59c.58-.45-1-1.27-1-2.09c10-5.22-8.98-4-8-4h4c-.98-0-2-1.22-2-2.5s3-9-4-9zm9-3h-1v1h1c1-0-2-1.22-2-2.5s13.98-12-13-12h9c-.98-0-2-1.22-2-2.5-0-.83.42-1.64-1-2.09v6.25c-1.09.53-2-1.84-2-3.25c6-11.31-7.55-13-9-13h4c1.45-0-3-1.69-3-3.5s14.5-6-13-6z'-data-evernote-id='108'-class='js-evernote-checked'%3e%3c/path%3e%3c/svg%3e)](https://app.yinxiang.com/outboundredirect.action?dest=https%3a%2f%2fwww.arcblock.io%2fzh%2fpost%2f2018%2f08%2f16%2findex-bitcoin%23%e8%ae%a1%e7%ae%97-block-hash-%e4%bb%a5%e5%8f%8a-transaction-hash)计算-block-hash-以及-transaction-hash" class="md-header-anchor"></a><a href='https://app.yinxiang.com/OutboundRedirect.action?dest=https%3A%2F%2Fwww.arcblock.io%2Fzh%2Fpost%2F2018%2F08%2F16%2Findex-bitcoin%23%E8%AE%A1%E7%AE%97-block-hash-%E4%BB%A5%E5%8F%8A-transaction-hash'><img src='data:image/svg+xml,%3csvg xmlns=&#39;http://www.w3.org/2000/svg&#39; aria-hidden=&#39;true&#39; focusable=&#39;false&#39; height=&#39;16&#39; version=&#39;1.1&#39; viewBox=&#39;0 0 16 16&#39; width=&#39;16&#39; data-evernote-id=&#39;107&#39; class=&#39;js-evernote-checked&#39;%3e%3cpath fill-rule=&#39;evenodd&#39; d=&#39;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&#39; data-evernote-id=&#39;108&#39; class=&#39;js-evernote-checked&#39;%3e%3c/path%3e%3c/svg%3e' alt='img' referrerPolicy='no-referrer' /></a><span>计算 Block Hash 以及 Transaction Hash</span></h4><p><span>Block 和 Transaction 的 Hash 值都是由相同的算法得出的，不同点只是在于参与计算的数据不同。对于 Block Hash 来讲，我们的输入数据只是 80 字节的 Block Header，而对于 Transaction 来讲是整个 Transaction 的数据。Block Header 里面是包括 Merkle Root 的，所以在计算 Block Hash 的时候不需要整个 Block 最为输入。</span></p><p><span>Hash 值是由输入数据进行两次 sha-256 运算得出的，其形式如下：</span></p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang=""><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang=""><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">hash = sha256(sha256(data))</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 22px;"></div><div class="CodeMirror-gutters" style="display: none; height: 22px;"></div></div></div></pre><p><span>我们分别将 Block Header 和整个 Transaction 带入上述公式便能得到相应的 Hash 值。</span></p><h4><a name="[![img](data:image/svg+xml,%3csvg-xmlns='http://www.w3.org/2000/svg'-aria-hidden='true'-focusable='false'-height='16'-version='1.1'-viewbox='0-0-16-16'-width='16'-data-evernote-id='113'-class='js-evernote-checked'%3e%3cpath-fill-rule='evenodd'-d='m4-9h1v1h4c-1.5-0-3-1.69-3-3.5s2.55-3-4-3h4c1.45-0-3-1.69-3-3.5-0-1.41-.91-2.72-2-3.25v8.59c.58-.45-1-1.27-1-2.09c10-5.22-8.98-4-8-4h4c-.98-0-2-1.22-2-2.5s3-9-4-9zm9-3h-1v1h1c1-0-2-1.22-2-2.5s13.98-12-13-12h9c-.98-0-2-1.22-2-2.5-0-.83.42-1.64-1-2.09v6.25c-1.09.53-2-1.84-2-3.25c6-11.31-7.55-13-9-13h4c1.45-0-3-1.69-3-3.5s14.5-6-13-6z'-data-evernote-id='114'-class='js-evernote-checked'%3e%3c/path%3e%3c/svg%3e)](https://app.yinxiang.com/outboundredirect.action?dest=https%3a%2f%2fwww.arcblock.io%2fzh%2fpost%2f2018%2f08%2f16%2findex-bitcoin%23%e8%ae%a1%e7%ae%97-block-height)计算-block-height" class="md-header-anchor"></a><a href='https://app.yinxiang.com/OutboundRedirect.action?dest=https%3A%2F%2Fwww.arcblock.io%2Fzh%2Fpost%2F2018%2F08%2F16%2Findex-bitcoin%23%E8%AE%A1%E7%AE%97-block-height'><img src='data:image/svg+xml,%3csvg xmlns=&#39;http://www.w3.org/2000/svg&#39; aria-hidden=&#39;true&#39; focusable=&#39;false&#39; height=&#39;16&#39; version=&#39;1.1&#39; viewBox=&#39;0 0 16 16&#39; width=&#39;16&#39; data-evernote-id=&#39;113&#39; class=&#39;js-evernote-checked&#39;%3e%3cpath fill-rule=&#39;evenodd&#39; d=&#39;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&#39; data-evernote-id=&#39;114&#39; class=&#39;js-evernote-checked&#39;%3e%3c/path%3e%3c/svg%3e' alt='img' referrerPolicy='no-referrer' /></a><span>计算 Block Height</span></h4><p><span>我们知道，Bitcoin 的原始数据是存储在 blkxxxxx.data 中的，每个 data 文件中都存放了许多个 Block。如果我们按顺序从第 0 字节开始读，把其中的所有 Block 都解析出来，我们会发现这些 Block 并不是完全有序的。换句话说你有可能先读出第 178 个 Block，然后才读出第 177 个 Block。至于为什么会这样，大家可以想想以前用 BT 下载的时候，那个进度条长什么样。</span></p><p><span>所以想要得到正确的 Block Height, 我们就必须对读出来的、无序的 Block 进行排序。了解 Blockchain 的同学应该知道，Blockchain 的数据结构是一个倒过来的单链表，我们可以通过 Block Hash 和 Previous Block Hash 来将这些 Block 重新串联起来。Genesis Block 的 Previous Block Hash 固定为全 0。这样一来我们也就可以得出 Block Height 了。</span></p><h4><a name="[![img](data:image/svg+xml,%3csvg-xmlns='http://www.w3.org/2000/svg'-aria-hidden='true'-focusable='false'-height='16'-version='1.1'-viewbox='0-0-16-16'-width='16'-data-evernote-id='118'-class='js-evernote-checked'%3e%3cpath-fill-rule='evenodd'-d='m4-9h1v1h4c-1.5-0-3-1.69-3-3.5s2.55-3-4-3h4c1.45-0-3-1.69-3-3.5-0-1.41-.91-2.72-2-3.25v8.59c.58-.45-1-1.27-1-2.09c10-5.22-8.98-4-8-4h4c-.98-0-2-1.22-2-2.5s3-9-4-9zm9-3h-1v1h1c1-0-2-1.22-2-2.5s13.98-12-13-12h9c-.98-0-2-1.22-2-2.5-0-.83.42-1.64-1-2.09v6.25c-1.09.53-2-1.84-2-3.25c6-11.31-7.55-13-9-13h4c1.45-0-3-1.69-3-3.5s14.5-6-13-6z'-data-evernote-id='119'-class='js-evernote-checked'%3e%3c/path%3e%3c/svg%3e)](https://app.yinxiang.com/outboundredirect.action?dest=https%3a%2f%2fwww.arcblock.io%2fzh%2fpost%2f2018%2f08%2f16%2findex-bitcoin%23%e8%ae%a1%e7%ae%97-addresses)计算-addresses" class="md-header-anchor"></a><a href='https://app.yinxiang.com/OutboundRedirect.action?dest=https%3A%2F%2Fwww.arcblock.io%2Fzh%2Fpost%2F2018%2F08%2F16%2Findex-bitcoin%23%E8%AE%A1%E7%AE%97-addresses'><img src='data:image/svg+xml,%3csvg xmlns=&#39;http://www.w3.org/2000/svg&#39; aria-hidden=&#39;true&#39; focusable=&#39;false&#39; height=&#39;16&#39; version=&#39;1.1&#39; viewBox=&#39;0 0 16 16&#39; width=&#39;16&#39; data-evernote-id=&#39;118&#39; class=&#39;js-evernote-checked&#39;%3e%3cpath fill-rule=&#39;evenodd&#39; d=&#39;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&#39; data-evernote-id=&#39;119&#39; class=&#39;js-evernote-checked&#39;%3e%3c/path%3e%3c/svg%3e' alt='img' referrerPolicy='no-referrer' /></a><span>计算 Addresses</span></h4><p><span>想要支持最开始的那种根据地址查询 Transaction 的功能，我们就必须求出每笔交易的发款人和收款人地址。这两个数据分别蕴含在 Transaction Input 和 Output 的</span><code>script</code><span>字段里面。求出地址的方法大致分为两步：</span></p><ol><li><span>对于 Transaction Output，找出其中的 public key 或 public key hash，再根据一定的算法计算出地址。</span></li><li><span>对于 Transaction Input, 从其对应的 Previous Transaction Output 中取出地址，作为它的地址。</span></li></ol><p><span>限于篇幅，我就不过多讨论第一点了。如果大家感兴趣，不妨告诉我们，我可以单独再用一期来讲解如何解析 Bitcoin Script。但是我想对第二点进行一些补充说明。虽然在 Transaction Input 里面你能拿到 public key，但是我不建议大家直接从 Transaction Input 里面计算地址。这是因为经过隔离见证之后，计算地址的方式发生了变化，直接从 Input 中解析地址很有可能得到一个与 Previous Transaction Output 不一样的地址。简单来说就是，你有可能以张三的名义收了一笔钱，却用李四的名义花掉了这笔钱。张三李四本来是同一个人，但是因为这个错误，计算账户余额的时候就会出现余额为负数的情况。</span></p><h2><a name="[![img](data:image/svg+xml,%3csvg-xmlns='http://www.w3.org/2000/svg'-aria-hidden='true'-focusable='false'-height='16'-version='1.1'-viewbox='0-0-16-16'-width='16'-data-evernote-id='126'-class='js-evernote-checked'%3e%3cpath-fill-rule='evenodd'-d='m4-9h1v1h4c-1.5-0-3-1.69-3-3.5s2.55-3-4-3h4c1.45-0-3-1.69-3-3.5-0-1.41-.91-2.72-2-3.25v8.59c.58-.45-1-1.27-1-2.09c10-5.22-8.98-4-8-4h4c-.98-0-2-1.22-2-2.5s3-9-4-9zm9-3h-1v1h1c1-0-2-1.22-2-2.5s13.98-12-13-12h9c-.98-0-2-1.22-2-2.5-0-.83.42-1.64-1-2.09v6.25c-1.09.53-2-1.84-2-3.25c6-11.31-7.55-13-9-13h4c1.45-0-3-1.69-3-3.5s14.5-6-13-6z'-data-evernote-id='127'-class='js-evernote-checked'%3e%3c/path%3e%3c/svg%3e)](https://app.yinxiang.com/outboundredirect.action?dest=https%3a%2f%2fwww.arcblock.io%2fzh%2fpost%2f2018%2f08%2f16%2findex-bitcoin%23%e7%bb%93%e8%af%ad)结语" class="md-header-anchor"></a><a href='https://app.yinxiang.com/OutboundRedirect.action?dest=https%3A%2F%2Fwww.arcblock.io%2Fzh%2Fpost%2F2018%2F08%2F16%2Findex-bitcoin%23%E7%BB%93%E8%AF%AD'><img src='data:image/svg+xml,%3csvg xmlns=&#39;http://www.w3.org/2000/svg&#39; aria-hidden=&#39;true&#39; focusable=&#39;false&#39; height=&#39;16&#39; version=&#39;1.1&#39; viewBox=&#39;0 0 16 16&#39; width=&#39;16&#39; data-evernote-id=&#39;126&#39; class=&#39;js-evernote-checked&#39;%3e%3cpath fill-rule=&#39;evenodd&#39; d=&#39;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&#39; data-evernote-id=&#39;127&#39; class=&#39;js-evernote-checked&#39;%3e%3c/path%3e%3c/svg%3e' alt='img' referrerPolicy='no-referrer' /></a><span>结语</span></h2><p><span>经过上述的所有步骤，我们就完成了 OCAP 数据预处理的第一步 -- 解析数据。之后我们还需要重新演绎 Bitcoin 的历史来获取整个 UTXO Pool 以及对每个地址做一些统计，如它们进行的所有交易数量和它们的余额等。</span></p><p><span>最后我想说，中本聪作为 Blockchain 之父，在设计 Bitcoin 的时候有许多奇思妙想，他对那些细节的把控真是让人叹为观止。在这个项目中，我也是一边学一边做，而且学到的东西真不是三言两语能说完。如果各位有时间，不妨写一些代码试着解析一下原始数据，这对彻底理解 Bitcoin 有很大的帮助。</span></p></div>
</body>
</html>